<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use App\Support\CustomForm\AttributeCruiseSupport;
use App\Support\CustomForm\AttributeSupport;

class CruiseTaskProjectModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = 'cruise_task_project';

    protected $fillable = [
        'id',
        'name',
        'categoryId',
        'status',
        'tipsType',
        'tipsContent',
        'inputType',
        'inputField',
        'inputObject',
        'meanType',
        'richShow',
    ];

    const STATUS_OFF = 0;
    const STATUS_ON  = 1;

    /**
     * tips_type支持的枚举
     */
    const TIPS_TYPES = [
        '',
        AttributeSupport::INPUT_TYPE_STRING,
        AttributeSupport::INPUT_TYPE_IMG_GROUP,
        AttributeSupport::INPUT_TYPE_STRING_IMG_GROUP,
    ];

    /**
     * input_type支持的枚举
     */
    const INPUT_TYPES = [
        AttributeSupport::INPUT_TYPE_SELECT,
        AttributeSupport::INPUT_TYPE_SELECT_MULTIPLE,
        AttributeSupport::INPUT_TYPE_STRING,
        AttributeSupport::INPUT_TYPE_TEXT,
        AttributeSupport::INPUT_DATE,
        AttributeSupport::INPUT_TYPE_IMG_GROUP,
        AttributeSupport::INPUT_VIDEO,
        AttributeCruiseSupport::INPUT_TYPE_BUSINESS_THREE,
        AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK,
        AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_SKIP,
        AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY,
        AttributeCruiseSupport::INPUT_TYPE_SIGNATURE,
        AttributeCruiseSupport::INPUT_TYPE_LOCATION,
        AttributeCruiseSupport::INPUT_TYPE_PAYEE_INFO,
        AttributeCruiseSupport::INPUT_TYPE_FEE_DELAY,
        AttributeCruiseSupport::INPUT_TYPE_RECTIFY_AGAINST,
        AttributeCruiseSupport::INPUT_TYPE_UPDATE_KF_SYSTEM,
        AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_FRONT,
        AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_BACK,
        AttributeCruiseSupport::INPUT_TYPE_RICH_SHOW,
        AttributeCruiseSupport::INPUT_TYPE_PROVINCE_CITY_AREA,
        AttributeCruiseSupport::INPUT_TYPE_BUSINESS_LICENSE,
        AttributeCruiseSupport::INPUT_TYPE_DATE_RANGE,
        AttributeCruiseSupport::INPUT_TYPE_IMG_VIDEO,
        AttributeCruiseSupport::INPUT_TYPE_IMG_FILE,
        AttributeCruiseSupport::INPUT_TYPE_APPROVAL_REJECT,
    ];

    const MEAN_TYPE_NULL    = 0;
    const MEAN_TYPE_PHONE   = 1;
    const MEAN_TYPE_ADDRESS = 2;
    const MEAN_TYPE_EMAIL   = 3;
    const MEAN_TYPE_WECHAT  = 4;
    const MEAN_TYPE_NAME    = 5;
    const MEAN_TYPE_ID_CARD = 6;
    const MEAN_TYPE_ID_CARD_FRONT = 7;
    const MEAN_TYPE_ID_CARD_BACK = 8;
    const MEAN_TYPE_PROVINCE_CITY_AREA = 9;
    const MEAN_TYPE_BANK_CARD_NO = 10;
    const MEAN_TYPE_BANK_CARD_OPENING_BANK = 11;
    const MEAN_TYPE_BANK_CARD_USER_NAME = 12;
    const MEAN_TYPE_MERCHANT_NAME          = 13;
    const MEAN_TYPE_BUSINESS_LICENSE_NO    = 14;
    const MEAN_TYPE_BUSINESS_LICENSE_VALID = 15;
    const MEAN_TYPE_REMARK = 16;
    const MEAN_TYPE_IDENTITY_VALID = 17;
    const MEAN_TYPE_BUSINESS_LICENSE_IMG = 18;
    const MEAN_TYPE_ID_CARD_FRONT_PRIVATE = 19;
    const MEAN_TYPE_ID_CARD_BACK_PRIVATE = 20;
    const MEAN_TYPE_RISK_TYPE_FIRST_BUILD = 21;
    const MEAN_TYPE_RISK_TYPE_FIRST_BUG = 22;
    const MEAN_TYPE_RISK_TYPE_FIRST_CLEAN = 23;
    const MEAN_TYPE_RISK_TYPE_FIRST_OPERATE = 24;
    const MEAN_TYPE_RISK_TYPE_FIRST_EXT = 25;
    const MEAN_TYPE_SHOP_CODE_NAME = 26;
    const MEAN_TYPE_SHOP_BRANCH = 27;
    const MEAN_TYPE_SUBMIT_USER_PRIVACY = 28;

    const MEAN_TYPE_TEXT = [
        self::MEAN_TYPE_NULL                   => '无',
        self::MEAN_TYPE_PHONE                  => '手机号',
        self::MEAN_TYPE_ADDRESS                => '详细住址',
        self::MEAN_TYPE_EMAIL                  => '邮箱',
        self::MEAN_TYPE_WECHAT                 => '微信',
        self::MEAN_TYPE_NAME                   => '姓名',
        self::MEAN_TYPE_ID_CARD                => '身份证号',
        self::MEAN_TYPE_ID_CARD_FRONT          => '身份证人像面（公有）',
        self::MEAN_TYPE_ID_CARD_BACK           => '身份证国徽面（公有）',
        self::MEAN_TYPE_PROVINCE_CITY_AREA     => '省市区',
        self::MEAN_TYPE_BANK_CARD_NO           => '银行卡号',
        self::MEAN_TYPE_BANK_CARD_OPENING_BANK => '银行卡开户行',
        self::MEAN_TYPE_BANK_CARD_USER_NAME    => '银行卡账户名称',
        self::MEAN_TYPE_MERCHANT_NAME          => '商户名称',
        self::MEAN_TYPE_BUSINESS_LICENSE_NO    => '营业执照编号',
        self::MEAN_TYPE_REMARK                 => '备注',
        self::MEAN_TYPE_BUSINESS_LICENSE_VALID => '营业执照有效期',
        self::MEAN_TYPE_IDENTITY_VALID         => '身份证有效期',
        self::MEAN_TYPE_BUSINESS_LICENSE_IMG   => '营业执照图片',
        self::MEAN_TYPE_ID_CARD_FRONT_PRIVATE  => '身份证人像面（私有）',
        self::MEAN_TYPE_ID_CARD_BACK_PRIVATE   => '身份证国徽面（私有）',
        self::MEAN_TYPE_RISK_TYPE_FIRST_BUILD   => '建筑结构',
        self::MEAN_TYPE_RISK_TYPE_FIRST_BUG     => '虫害活动',
        self::MEAN_TYPE_RISK_TYPE_FIRST_CLEAN   => '清洁卫生',
        self::MEAN_TYPE_RISK_TYPE_FIRST_OPERATE => '运营管理',
        self::MEAN_TYPE_RISK_TYPE_FIRST_EXT     => '外部压力',
        self::MEAN_TYPE_SHOP_CODE_NAME          => '门店信息', // 5233夸父炸串（上海嘉定永盛里店）
        self::MEAN_TYPE_SHOP_BRANCH             => '门店分公司', // 上海省区
        self::MEAN_TYPE_SUBMIT_USER_PRIVACY     => '需求提交人隐私信息', // 胡**18****3521
    ];

    // meanType支持的组件
    const MEAN_TYPE_RELATION_MODULE = [
        self::MEAN_TYPE_RISK_TYPE_FIRST_BUILD => [AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY],
        self::MEAN_TYPE_RISK_TYPE_FIRST_BUG => [AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY],
        self::MEAN_TYPE_RISK_TYPE_FIRST_CLEAN => [AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY],
        self::MEAN_TYPE_RISK_TYPE_FIRST_OPERATE => [AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY],
        self::MEAN_TYPE_RISK_TYPE_FIRST_EXT => [AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY],
    ];

    private ?AttributeCruiseSupport $attributeCruiseSupport = null;

    public function getStatusTextAttribute(): string
    {
        return $this->status == CruiseTaskProjectModel::STATUS_ON ? '启用' : '停用';
    }

    public function getInputTypeTextAttribute(): string
    {
        switch ($this->inputType) {
            case AttributeSupport::INPUT_TYPE_STRING:
                $str = '单行文本';
                break;
            case AttributeSupport::INPUT_TYPE_TEXT:
                $str = '多行文本';
                break;
            case AttributeSupport::INPUT_TYPE_SELECT:
                $str = '单选';
                break;
            case AttributeSupport::INPUT_TYPE_SELECT_MULTIPLE:
                $str = '多选';
                break;
            case AttributeSupport::INPUT_DATE:
                $str = '日期';
                break;
            case AttributeSupport::INPUT_TYPE_IMG_GROUP:
                $str = "图片";
                break;
            case AttributeSupport::INPUT_VIDEO:
                $str = "视频";
                break;
            case AttributeCruiseSupport::INPUT_TYPE_BUSINESS_THREE:
                $str = '开业三天组件';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK:
                $str = '巡检';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_SKIP:
                $str = '巡检 - 不适用';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY:
                $str = '巡检 - 生物防治';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_SIGNATURE:
                $str = '签名';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_LOCATION:
                $str = '定位';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_PAYEE_INFO:
                $str = '收款信息';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_FEE_DELAY:
                $str = '年费是否延期';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_RECTIFY_AGAINST:
                $str = '是否有整改单违约函';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_UPDATE_KF_SYSTEM:
                $str = '统一更新夸父收银组件';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_FRONT:
                $str = '身份证人像面';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_BACK:
                $str = '身份证国徽面';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_RICH_SHOW:
                $str = '富文本展示';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_PROVINCE_CITY_AREA:
                $str = '省市区';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_BUSINESS_LICENSE:
                $str = '营业执照';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_DATE_RANGE:
                $str = '日期范围选择';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_IMG_VIDEO:
            case AttributeCruiseSupport::INPUT_TYPE_IMG_FILE:
                $str = '文件多选';
                break;
            case AttributeCruiseSupport::INPUT_TYPE_APPROVAL_REJECT:
                $str = '审批驳回组件';
                break;
            default:
                $str = '未配置描述';
                break;
        }

        return $str;
    }

    public function getOptionsArrAttribute(): array
    {
        if (!$this->attributeCruiseSupport) {
            $this->attributeCruiseSupport = unserialize($this->inputObject);
        }

        if (!$this->attributeCruiseSupport->customAttributes) {
            return [];
        }

        return $this->attributeCruiseSupport->customAttributes['options'] ?? [];
    }

    public function getRequiredAttribute(): bool
    {
        if (!$this->attributeCruiseSupport) {
            $this->attributeCruiseSupport = unserialize($this->inputObject);
        }

        if (!$this->attributeCruiseSupport->validateSupport) {
            return true;
        }

        return $this->attributeCruiseSupport->validateSupport->getRequired() ?? true;
    }

    public function getMaxContentLengthAttribute(): int
    {
        if (!$this->attributeCruiseSupport) {
            $this->attributeCruiseSupport = unserialize($this->inputObject);
        }

        if (!$this->attributeCruiseSupport->validateSupport) {
            return 100;
        }

        return $this->attributeCruiseSupport->validateSupport->getMax() ?? 100;
    }

    public function getAuditValueAttribute(): array
    {
        if (!$this->attributeCruiseSupport) {
            $this->attributeCruiseSupport = unserialize($this->inputObject);
        }

        if (!$this->attributeCruiseSupport->customAttributes) {
            return [];
        }

        return $this->attributeCruiseSupport->customAttributes['auditValue'] ?? [];

    }
}
